﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace BusinessLogic
{
    public class ChiTietDonHangNCC
    {
        public Entities.ChiTietDonHangNCC[] SelectChiTietDonHangNCC(string madhhncc)
        {
            SqlConnection con = DataAccess.Connection.OpenConnection();
            DataTable dt = new DataTable();
            Entities.ChiTietDonHangNCC[] ctddhncc = null;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "sp_ChiTietDonHangNCC_Select";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = con;

                cmd.Parameters.Add("@MaDonDatHangNCC", SqlDbType.VarChar, 50).Value = madhhncc;

                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();

                adapter.Fill(ds, "ctddhncc");
                dt = ds.Tables["ctddhncc"];

                var temp = from ddh in dt.AsEnumerable()
                           select new Entities.ChiTietDonHangNCC
                           {
                               MaDonDatHangNCC = ddh.Field<string>("MaDonDatHangNCC"),
                               MaHangHoa = ddh.Field<string>("MaHangHoa"),
                               TenHangHoa = ddh.Field<string>("TenHangHoa"),
                               SoLuong = ddh.Field<string>("SoLuong"),
                               Gia = ddh.Field<string>("DonGia"),
                               PTTrietKhau = ddh.Field<string>("PhanTramChietKhau"),
                               TongTien = ddh.Field<string>("ThanhTien"),
                               Thue = ddh.Field<string>("Thue"),
                               GhiChu = ddh.Field<string>("GhiChu"),
                               Deleted = ddh.Field<bool>("Deleted"),
                           };
                ctddhncc = temp.ToArray<Entities.ChiTietDonHangNCC>();
            }
            catch
            {
                ctddhncc = new Entities.ChiTietDonHangNCC[0];
            }
            finally
            {
                con.Close();
            }
            return ctddhncc;
        }
        public bool InsertChiTietDonHangNCC(Entities.ChiTietDonHangNCC[] ctddhncc, Entities.DonDatHangNCC ddhncc)
        {
            SqlConnection con = DataAccess.Connection.OpenConnection();
            DataTable dt = new DataTable();
            bool i = false;

            try
            {
                SqlTransaction tran = con.BeginTransaction("tran");
                try
                {
                    SqlCommand acmd = new SqlCommand();
                    acmd.CommandText = "sp_DonHangNCC_Insert";
                    acmd.CommandType = CommandType.StoredProcedure;
                    acmd.Connection = con;

                    acmd.Parameters.Add("@MaDonDatHangNCC", SqlDbType.VarChar, 50).Value = ddhncc.MaDonDatHangNCC;
                    acmd.Parameters.Add("@NgayDonHang", SqlDbType.DateTime).Value = ddhncc.NgayDonHang;
                    acmd.Parameters.Add("@MaNhaCungCap", SqlDbType.VarChar, 50).Value = ddhncc.MaNhaCungCap;
                    acmd.Parameters.Add("@TenNhaCungCap", SqlDbType.NVarChar, 200).Value = ddhncc.TenNhaCungCap;
                    acmd.Parameters.Add("@NoHienThoi", SqlDbType.Float).Value = ddhncc.NoHienThoi;
                    acmd.Parameters.Add("@TrangThaiDonDatHang", SqlDbType.NVarChar, 20).Value = ddhncc.TrangThaiDonDatHang;
                    acmd.Parameters.Add("@NgayNhapDuKien", SqlDbType.DateTime).Value = ddhncc.NgayNhapDuKien;
                    acmd.Parameters.Add("@HinhThucThanhToan", SqlDbType.NVarChar, 20).Value = ddhncc.HinhThucThanhToan;
                    acmd.Parameters.Add("@MaKho", SqlDbType.VarChar, 50).Value = ddhncc.MaKho;
                    acmd.Parameters.Add("@TenKho", SqlDbType.NVarChar, 200).Value = ddhncc.TenKho;
                    acmd.Parameters.Add("@MaNhanVien", SqlDbType.VarChar, 50).Value = ddhncc.MaNhanVien;
                    acmd.Parameters.Add("@TenNhanVien", SqlDbType.NVarChar, 200).Value = ddhncc.TenNhanVien;
                    acmd.Parameters.Add("@MaTienTe", SqlDbType.VarChar, 50).Value = ddhncc.MaTienTe;
                    acmd.Parameters.Add("@Tygia", SqlDbType.NVarChar, 200).Value = ddhncc.Tygia;
                    acmd.Parameters.Add("@GhiChu", SqlDbType.VarChar, 50).Value = ddhncc.GhiChu;
                    acmd.Parameters.Add("@Deleted", SqlDbType.Bit).Value = ddhncc.Deleted;

                    acmd.ExecuteNonQuery();

                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandText = "sp_ChiTietDonHangNCC_Insert";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Connection = con;

                    foreach (Entities.ChiTietDonHangNCC item in ctddhncc)
                    {
                        cmd.Parameters.Clear();
                        cmd.Parameters.Add("@MaDonDatHangNCC", SqlDbType.VarChar, 50).Value = item.MaDonDatHangNCC;
                        cmd.Parameters.Add("@MaHangHoa", SqlDbType.VarChar, 50).Value = item.MaHangHoa;
                        cmd.Parameters.Add("@TenHangHoa", SqlDbType.NVarChar, 200).Value = item.TenHangHoa;
                        cmd.Parameters.Add("@SoLuong", SqlDbType.Float).Value = item.SoLuong;
                        cmd.Parameters.Add("@DonGia", SqlDbType.Float).Value = item.Gia;
                        cmd.Parameters.Add("@PhanTramChietKhau", SqlDbType.Float).Value = item.PTTrietKhau;
                        cmd.Parameters.Add("@ThanhTien", SqlDbType.Float).Value = item.TongTien;
                        cmd.Parameters.Add("@Thue", SqlDbType.Float, 20).Value = item.Thue;
                        cmd.Parameters.Add("@GhiChu", SqlDbType.NVarChar, 200).Value = item.GhiChu;
                        cmd.Parameters.Add("@Deleted", SqlDbType.Bit, 20).Value = item.Deleted;

                        cmd.ExecuteNonQuery();
                    }
                    
                    tran.Commit();
                    i = true;
                }
                catch (Exception)
                {

                    tran.Rollback("tran");
                }

                LuuVietHeThong.LuuViet("Insert", ddhncc.TaiKhoan.MaNhanVien, "Thêm đơn đặt hàng: " + ddhncc.MaDonDatHangNCC, ddhncc.TaiKhoan.TenDangNhap, DateTime.Now.ToString("hh:mm dd/MM/yyyy"));
            }
            catch
            {
                
                i = false;
            }
            finally
            {
                con.Close();
            }
            return i;
        }
    }
}
